#!/usr/bin/env python
# -*- coding: utf-8 -*-

from core.deck import deck
from core.poker import poker
from core.sort import sort
from game.FAL.FightAgainstLandlords import FALDeck
import random

'''
game.FAL.FALPlay(FALDeck)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
玩家出牌
'''
class FALPlay(FALDeck):

    '''
    @出牌
    '''
    def play(self,owner,ownerplaylist):
        '''
        Feature
            play(self,owner,ownerplaylist)
        Description
            玩家出牌
        Args
            owner:
                Type:string
                description:出牌玩家
            ownerplaylist:
                Type:[]
                description:玩家出牌的牌面
        '''
        if ownerplaylist:
            return self.checkplaylist(owner,ownerplaylist)
    '''
    @获取玩家手牌数量
    '''        
    def getpokercount(self,owner):
        '''
        Feature
            getpokercount(self,owner)
        Description
            获取玩家手牌数量
        Args
            owner:
                type:string
                description:玩家
        Return
            int:玩家手牌数量
        '''
        return len(self.__player__[owner])

    def checkplaylist(self,owner,ownerplaylist):
        '''
        Feature
            checkplaylist(self,owner,ownerplaylist)
        Description
            检查玩家的出牌是否合法
        Args
            owner:
                Type:string
                description:出牌玩家
            ownerplaylist:
                Type:[]
                description:玩家出牌的牌面
        '''
        pl=[]
        playtype=None
        for item in sorted(ownerplaylist,reverse=True):
                pl.append(self.__player__[owner][item])
        playtype=FALPlayPokerType(owner,pl)
        result=False
        if playtype:
            lastplay=None #上手出牌
            if len(self.__playlist__)>0:
                lastplay=self.__playlist__[len(self.__playlist__)-1]
            if lastplay:
                if lastplay.__owner__!= playtype.__owner__:
                    if playtype.__type__==lastplay.__type__:
                        if playtype.__value__>lastplay.__value__:
                            result=True
                    elif playtype.__type__=="bomb":
                        result=True                
                else:
                    if playtype.__type__:
                        result=True
            else:
                if playtype.__type__:
                    result=True
            if result:
                self.__playlist__.append(playtype)
                for item in sorted(ownerplaylist,reverse=True):
                    del self.__player__[owner][item]
        return result

'''
game.FAL.analysis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
分析扑克组合
'''
class analysis:
    def __init__(self,owner,playlist):
        '''
        Feature
            __init__(self,owner,playlist)
        Description
            初始化
        Args
            owner:
                Type:string
                description:出牌玩家
            playlist:
                Type:[]
                description:玩家出牌的牌面
        Attribute
            private __owner__
                type:string
                description:出牌玩家
            private __playlist__
                type:[]
                description:玩家出的牌牌面
            private __single__
                type:[]
                description:单张扑克列表
            private __apair__
                type:[]
                description:对子列表
            private __threeofakind__
                type:[]
                description:三张列表
            private __fourofakind__=[]
                type:[]
                description:四张列表
            private __type__
                type:string
                description:牌型(A->单张,ABCDE->顺子,AA->对子,AAA->三不带,AAAB->三带一,AAABB->三带一对,AAABBBCD->飞机带单张,AAABBBCCDD->飞机带对子,AAAABC->四带二张单,AAAABB->四带一对,AAAA->炸弹)
            private __value__
                type:int
                description:总分值
        '''
        self.__owner__=owner
        self.__playlist__=sort().sortasc(playlist) #进行排序
        self.__num__=len(self.__playlist__)
        self.__single__=[]
        self.__apair__=[]
        self.__threeofakind__=[]
        self.__fourofakind__=[]
        self.__type__=None
        self.__value__=0
        #归类牌型
        sameoffsetlist=[] #记录相同牌面偏移量
        for offset in range(0,len(self.__playlist__)):
            if offset not in sameoffsetlist:
                same=1 #计数相同纸牌数量
                for offset2 in range(offset+1,len(self.__playlist__)):
                    if(self.__playlist__[offset].__pointnum__==self.__playlist__[offset2].__pointnum__):
                        same=same+1
                        sameoffsetlist.append(offset2)
                if same==1:
                    self.__single__.append(self.__playlist__[offset].__pointnum__)                        
                elif same==2:
                    self.__apair__.append(self.__playlist__[offset].__pointnum__)
                elif same==3:
                    self.__threeofakind__.append(self.__playlist__[offset].__pointnum__)
                elif same==4:
                    self.__fourofakind__.append(self.__playlist__[offset].__pointnum__)

'''
game.FAL.FALPlayPokerType(analysis)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
分析牌型
'''
class FALPlayPokerType(analysis):
        def __init__(self,owner,playlist):
            super(FALPlayPokerType,self).__init__(owner,playlist) 
            self.calculate()

        def calculate(self):
            #计算牌型
            sum=0
            offset=0
            if len(self.__single__)>0:
                if len(self.__single__)==1:
                    if len(self.__single__)==len(self.__playlist__):
                        self.__type__="A" #单张
                if len(self.__single__)==2:
                    if len(self.__single__)==len(self.__playlist__):
                        if self.__single__[0]==16 and self.__single__[1]==17:
                            self.__type__="AAAA" #炸弹
                if len(self.__single__)>=5:
                    if len(self.__single__)==len(self.__playlist__):
                        for item in range(0,len(self.__single__)):
                            sum=sum+self.__single__[item]
                            offset=offset+item
                        if self.__single__[0]==(sum-offset)/len(self.__single__):
                            self.__type__="ABCDE" #顺子
            if len(self.__apair__)>0:
                if len(self.__apair__)==1:
                    if len(self.__apair__)==len(self.__playlist__)/2:
                        self.__type__="AA" #单对
                elif len(self.__apair__)>=3:
                    sum=0
                    offset=0
                    if len(self.__apair__)==len(self.__playlist__)/2:
                        for item in range(0,len(self.__apair__)):
                            sum=sum+self.__apair__[item]
                            offset=offset+item
                        if self.__apair__[0]==(sum-offset)/len(self.__apair__):
                            self.__type__="AABBCC" #连对
            if len(self.__threeofakind__)>0:
                if len(self.__threeofakind__)==1:
                    if len(self.__single__)==1 and len(self.__playlist__)==4:
                        self.__type__="AAAB" #三带一
                    elif len(self.__apair__)==1 and len(self.__playlist__)==5:
                        self.__type__="AAABB" #三带二
                    elif len(self.__single__)==0 and len(self.__apair__)==0:
                        self.__type__="AAA" #三不带
                if len(self.__threeofakind__)>=2:
                    sum=0
                    offset=0
                    for item in range(0,len(self.__threeofakind__)):
                        sum=sum+self.__threeofakind__[item]
                        offset=offset+item
                    if self.__threeofakind__[0]==(sum-offset)/len(self.__threeofakind__):
                        if len(self.__single__)==len(self.__threeofakind__) and len(self.__playlist__)==len(self.__threeofakind__)*3+len(self.__single__):
                            self.__type__="AAABBBCD" #飞机带单张
                        elif len(self.__apair__)==len(self.__threeofakind__)  and len(self.__playlist__)==len(self.__threeofakind__)*3+len(self.__apair__)*2:
                            self.__type__="AAABBBCCDD" #飞机带对子
            if len(self.__fourofakind__)==1:
                if len(self.__single__)==2 and len(self.__playlist__)==6:
                    self.__type__="AAAABC" #四带2
                elif len(self.__apair__)==1 and len(self.__playlist__)==6:
                    self.__type__="AAAABB" #四带一对
                elif len(self.__threeofakind__)==0 and len(self.__playlist__)==4:
                    self.__type__="AAAA" #炸弹
            #汇总分值(分值最好移到牌型里计算，做加权)
            if self.__type__:
                for item in self.__playlist__:
                    self.__value__=self.__value__+item.__value__

